package com.navi.loaddata.loadService.impl.PanelData;

import com.navi.loaddata.core.gpEntity.CaldDayShift;
import com.navi.loaddata.core.gpEntity.MinMaxEvttimeStamp;
import com.navi.loaddata.core.gpEntity.ShiftStartEndTime;
import com.navi.loaddata.core.gpEntity.YearMonth;
import com.navi.loaddata.core.mysqlEntity.EtlConfD;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author fuchenyang
 * @date 2019/12/05
 */
@Service
public class LoadPanelDataByShift_CService {
    private static final Logger logger = LoggerFactory.getLogger(LoadPanelDataByShift_CService.class);
    @Autowired
    private LoadPanelDataByShiftBean loadPanelDataByShiftBean;


    public String[] loadPanelDataMain(EtlConfDModel etlConfDModel, EtlConfD etlConfD) throws Exception {
        String[] resMsg = loadPanelData(etlConfDModel, etlConfD);
        logger.info("loadPanelDataByShift_C loadPanelDataMain finish!");
        return resMsg;
    }

    private String[] loadPanelData(EtlConfDModel etlConfDModel, EtlConfD etlConfD) throws Exception {
        String[] resMsg = new String[3];
        int okCnt = 0;
        int errCnt = 0;
        String errMsg = null;

        //找到需要插入数据的表
        String tmpTab = "wpp_cdefect_panel_shift_sum";
        String rawTable = "wpp_cdefect_panel_f";

        List<YearMonth> yearMonthList = loadPanelDataByShiftBean.getYearMonth(etlConfDModel, rawTable);
        List<YearMonth> yearMonthNewList = new ArrayList<>();

        //panel表中查不到对应的日期，则说明没有对应数据，返回为空
        if (yearMonthList.size() == 0) {
            resMsg[0] = "0";
            resMsg[1] = "0";
            resMsg[2] = "查询时间" + etlConfDModel.getRunStartTimestamp() + "到" + etlConfDModel.getRunEndTimestamp() + "内，无符合Glass命名规则的glass_id";

            return resMsg;
        } else {
            //去除不符合规则的数据，若yearMonthList还有数据则继续后续逻辑，若yearMonthList无数据，则返回
            for (YearMonth yearMonth : yearMonthList) {
                if (yearMonth.getMonth() != null && yearMonth.getYear() != null) {
                    if (yearMonth.getYear().matches("[0-9]{2,}") && (yearMonth.getMonth().matches("[1-9]") || yearMonth.getMonth().matches("[A-C]"))) {
                        yearMonthNewList.add(yearMonth);
                    }
                }
            }

            if (yearMonthNewList.size() == 0) {
                resMsg[0] = "0";
                resMsg[1] = "0";
                resMsg[2] = "查询时间" + etlConfDModel.getRunStartTimestamp() + "到" + etlConfDModel.getRunEndTimestamp() + "内，无符合Glass命名规则的glass_id";

                return resMsg;
            }
        }

        //获取汇总班别时rework数据上一次同站点同Panel所在的天和班别
        List<CaldDayShift> caldDayShiftList = loadPanelDataByShiftBean.getPreReworkShf(etlConfDModel, rawTable, yearMonthNewList);

        if (caldDayShiftList.size() > 0) {
            //删除汇总表内rework之前对应天、班别的全部数据
            loadPanelDataByShiftBean.deleteReworkData(etlConfDModel, tmpTab, caldDayShiftList, errMsg, errCnt);

            //by天、班别，将rework之前对应班别的数据重新汇总
            for (CaldDayShift caldDayShift : caldDayShiftList) {
                ShiftStartEndTime shiftStartEndTime = loadPanelDataByShiftBean.getShiftTime(caldDayShift.getCald_date(), caldDayShift.getShf_key_fk());

                loadPanelDataByShiftBean.insertData(etlConfDModel, tmpTab, shiftStartEndTime.getStartTime(), shiftStartEndTime.getEndTime(), errMsg, okCnt, errCnt);
            }
        }

        //以跑数据结束时间(run_end_timestamp)来得出对应班别的最小和最大时间
        MinMaxEvttimeStamp minMaxEvtTime= loadPanelDataByShiftBean.getMinMaxEvtTime(etlConfDModel.getRunEndTimestamp());

        //删除之前汇总的数据
        loadPanelDataByShiftBean.deleteData(etlConfDModel, tmpTab, minMaxEvtTime.getMinTime(), minMaxEvtTime.getMaxTime(), errMsg, errCnt);

        //将对应班别的数据汇总插入到汇总表
        loadPanelDataByShiftBean.insertData(etlConfDModel, tmpTab, minMaxEvtTime.getMinTime(), minMaxEvtTime.getMaxTime(), errMsg, okCnt, errCnt);

        resMsg[0] = String.valueOf(okCnt);
        resMsg[1] = String.valueOf(errCnt);
        resMsg[2] = errMsg;

        return resMsg;
    }
}
